import { Component, OnInit, ViewChild } from '@angular/core';
import { NzFormatEmitEvent } from 'ng-zorro-antd/core';
import { DatePipe } from '@angular/common';
import { RoleManageService } from '../../system-manage/role-manage/role-manage.service';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalService } from 'ng-zorro-antd/modal';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { NzTreeComponent } from 'ng-zorro-antd';
import { HCommonService } from 'src/app/h-common/h-common.service';
import { WaitTasksService } from './wait-tasks.service';
@Component({
  selector: 'app-wait-tasks',
  templateUrl: './wait-tasks.component.html',
  styleUrls: ['./wait-tasks.component.scss']
})
export class WaitTasksComponent implements OnInit {
  @ViewChild("menuTreeComp", { static: false }) menuTree: NzTreeComponent
  searchValue: string = '';//模糊搜索
  dateRange = [];//日期选择
  pageIndex: number = 1;//提交的页码
  isOkLoading = false;
  isVisible = false;
  deptData = [];
  editId: number = -1;
  isCheckedAll: boolean = false;
  listOfAllData = [];
  isIndeterminate: boolean = false;
  private deleteRole = [];//删除的角色
  mapOfCheckedId: { [key: string]: boolean } = {};
  pagesize: number = 10;//table每页条数
  totalSize: any = '';//总共条数
  menuNodes = [];
  checkedMenu = [];
  selectedId: number = -1;//选中的行的id
  bornTasks = {}

  modalDomBox: any
  constructor(private roleManageService: RoleManageService,
    private datePipe: DatePipe,
    private message: NzMessageService,
    private modalService: NzModalService,
    private fb: FormBuilder,
    private commonService: HCommonService,
    private waitTasksService: WaitTasksService) { }

  ngOnInit() {
    this.modalDomBox = document.querySelector("#modal_box");

    /* 获取权限 */
    this.bornTasks = this.commonService.getRoles("task");
    this.getSearchList()
    this.getAllTreeList('')
  }
  // 日期选择
  onDateChange(result: Date): void {
    let startTime = this.formatDate(result[0])
    let endTime = this.formatDate(result[1])
    this.dateRange = [startTime + " 00:00:00", endTime + " 23:59:59"];
    this.pageIndex = 1;
    this.getSearchList();
  }
  // 时间格式化
  formatDate(date) {
    return this.datePipe.transform(date, 'yyyy-MM-dd');
  }
  hotSearch() {
    this.pageIndex = 1;
    this.getSearchList();
  }
  // 获取列表
  getSearchList() {
    var data = {
      page: this.pageIndex,
      size: this.pagesize,
      createTime: this.dateRange,
      blurry: this.searchValue
    }
    this.roleManageService.getRoles(data).subscribe(res => {
      this.listOfAllData = res.content
      this.totalSize = res.totalElements
    })
  }
  // 点击分页事件
  clickPage(pageIndex) {
    this.pageIndex = pageIndex;
    this.getSearchList()
  }

  // 初始化部门树
  initHtree(data) {
    if (data.length > 0) {
      for (let i = 0; i < data.length; i++) {
        data[i].title = data[i].name || data[i].lable;
        data[i].key = data[i].id || data[i].type;
        if (data[i].children) {
          this.initHtree(data[i].children)
        }
      }
    }
    return data
  }
  getAllTreeList(ids?) {
    this.waitTasksService.GetGroupTodos().subscribe(res => {
      res.forEach(res => res.children.forEach(w => {
        w.isLeaf = true;

      }));
      res.forEach(res => { res.disableCheckbox = true; res.expanded = true });
      if (ids) {
        let _data = this.bindMenuData(this.initHtree(res), ids);
        this.menuNodes = _data;
      } else {
        this.menuNodes = this.initHtree(res);
      }
    });
  }
  // nzEvent(event: NzFormatEmitEvent): void {
  // }
  //刷新菜单分配列表
  getTodo(data?) {
    if (data) {
      this.selectedId = data.id;
      let menus = data.toDos;
      let ids = [];
      for (let i = 0; i < menus.length; i++) {
        ids.push(menus[i].id)
      }
      this.getAllTreeList(ids);
    }
  }
  //绑定是否选中
  bindMenuData(data, editIds) {
    if (data.length > 0) {
      data.forEach(item => {
        if (editIds.indexOf(item.id) > -1) {
          item.checked = true;
        } else {
          item.checked = false;
        }
        if (item.children) {
          this.bindMenuData(item.children, editIds)
        }
      })
    }
    return data;
  }
//
  getAllCheckedMenu(data, arr) {
    if (data.length > 0) {
      data.forEach(item => {
        if (item._isChecked || item._isHalfChecked) {
          if (item.level == 1) {
            arr.push({
              id: item.key
            })
          }
          if (item.children) {
            this.getAllCheckedMenu(item.children, arr)
          }
        }

      })
    }
  }
  //保存数据
  saveMenuEdit() {
    let _checkedNodes = this.menuTree.getCheckedNodeList();
    let _checkedIds = [];
    this.getAllCheckedMenu(_checkedNodes, _checkedIds);

    let _dataJson = {
      id: this.selectedId,
      toDos: _checkedIds
    }
    console.log(_dataJson);
    this.modalService.confirm({
      nzTitle: '确定提交吗？',
      nzContent: '',
      nzOkText: '确认',
      nzCancelText: '取消',
      nzOnOk: () => {
        this.waitTasksService.WaitTasksTodo(_dataJson).subscribe(res => {
          this.selectedId = -1;
          this.getSearchList();
          this.getAllTreeList();
          this.message.info("修改成功")
        })
      }
    })
  }
}
